package org.jaronsource.sample.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.jaronsource.framework.core.dao.support.IGenericDao;
import org.jaronsource.framework.core.service.SearchFormSupportGenericService;
import org.jaronsource.sample.dao.SysFuncDao;
import org.jaronsource.sample.dao.SysRoleDao;
import org.jaronsource.sample.domain.SysFunc;
import org.jaronsource.sample.domain.SysRole;
import org.jaronsource.sample.service.SysRoleService;

@Service
public class SysRoleServiceImpl extends SearchFormSupportGenericService<SysRole, Integer> implements SysRoleService {

	@Autowired
	private SysRoleDao sysRoleDao;

	@Autowired
	private SysFuncDao sysFuncDao;

	@Override
	public IGenericDao<SysRole, Integer> getDao() {
		return sysRoleDao;
	}

	@Override
	@Transactional
	public void assignFunc( Integer roleId, Integer[] funcIds ) {

		SysRole sysRole = getDao().findReferenceByPk( roleId );

		List<SysFunc> functions = new ArrayList<SysFunc>();
		for ( Integer funcId : funcIds ) {
			SysFunc sysFunc = sysFuncDao.findReferenceByPk( funcId );
			functions.add( sysFunc );
		}

		sysRole.setFunctions( functions );

		getDao().save( sysRole );
	}

}